package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannerSevice;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @ClassName ChannelServiceImpl
 * @Description TODO
 * @Author AWei
 * @DATE 2021/4/9 17:36
 * @Version 1.0
 */
@Service
@Transactional
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannerSevice {

    /**
     * @param channelDto 前端传来频道参数
     * @return 分页查询结果
     * @Description 分页查询频道信息或者根据名称分页查询
     */
    @Override
    public ResponseResult queryChannelByPageOrName(ChannelDto channelDto) {
        //1. 检验参数：参数对象是否为空，分页数据是否存在，否则设为默认值
        if (channelDto == null) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        channelDto.checkParam();
        //2. 判断参数对象名称（name）是否为空，若不为空则添加模糊查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(channelDto.getName())) {
            queryWrapper.like(AdChannel::getName, channelDto.getName());
        }
        //3. 分页查询并返回结果对象
        Page<AdChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());
        IPage<AdChannel> pageResult = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }

    /**
     * @param adChannel 新增的频道对象
     * @return 新增结果
     * @Description 新增频道信息
     */
    @Override
    public ResponseResult addChannel(AdChannel adChannel) {
        //1. 检验参数：name（频道名称）和参数adChannel不能为null
        if (StringUtils.isBlank(adChannel.getName()) || ObjectUtils.isEmpty(adChannel)) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 判断新增频道是否设为默认频道，若是则修改原来的默认频道为非默认
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isEmpty(adChannel.getIsDefault())) {
            adChannel.setIsDefault(false);
        }
        if (adChannel.getIsDefault()) {
            AdChannel oldChannel = getOne(queryWrapper.eq(AdChannel::getIsDefault, 1));
            if (!ObjectUtils.isEmpty(oldChannel)) {
                oldChannel.setIsDefault(false);
                updateById(oldChannel);
            }
        }
        //3. 判断频道名称是否重复，若重复则返回错误信息
        AdChannel one = getOne(queryWrapper.eq(AdChannel::getName, adChannel.getName()));
        if (!ObjectUtils.isEmpty(one)) {
            CustomException.custom(AppHttpCodeEnum.DATA_EXIST);
        }
        //4. 添加频道信息并返回添加结果
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult();
    }

    /**
     * @param adChannel 被修改对象
     * @return 修改结果
     * @Description 频道修改和频道有效无效设置
     */
    @Override
    public ResponseResult modifyChannel(AdChannel adChannel) {
        //1. 检验参数：参数id变量和参数adChannel不能为null
        if (ObjectUtils.isEmpty(adChannel.getId()) || ObjectUtils.isEmpty(adChannel)) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 查询该频道信息是否存在
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            CustomException.custom(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //存在，修改频道信息
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    /**
     * @param id 被删除频道id
     * @return 删出结果
     * @Description 删除频道
     */
    @Override
    public ResponseResult deleteChannelById(Integer id) {
        //1. 检验参数：id（频道id）是否为空
        if (ObjectUtils.isEmpty(id)) {
            CustomException.custom(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2. 查询并判断该频道是否为空
        AdChannel channel = getById(id);
        if (ObjectUtils.isEmpty(channel)) {
            CustomException.custom(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3. 判断是否为默认频道
        if (channel.getIsDefault()) {
            CustomException.custom(AppHttpCodeEnum.DEFAULT_CHANNEL);
        }
        //4. 判断频道是否为开启状态
        if (channel.getStatus()) {
            CustomException.custom(AppHttpCodeEnum.VALID_CHANNEL);
        }
        //5. 删除该频道
        removeById(id);
        return ResponseResult.okResult();
    }

}
